/**
 * ExportEmpInfoDAO class.
 */
package rai.util;

import java.io.File;
import java.util.List;
import java.util.Locale;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.Orientation;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import rai.dto.EmployeeDTO;

public class ExportDataUtil {
    private static Logger log = Logger.getLogger("PITSPortlet");

	public void export(List<EmployeeDTO> searchResultList) {
	    log.info("Start Export");
		JFileChooser fc = new JFileChooser();
		fc.addChoosableFileFilter(new ExcelFileFilter());
		File xlsFile = null;
		int returnVal;

		fc.requestFocusInWindow();

		returnVal = fc.showSaveDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			xlsFile = fc.getSelectedFile();

			log.info("Selected file: " + xlsFile.getName());

			writeToFile(searchResultList, xlsFile);
		}
		else {
			log.info("Save command cancelled by user.");
			fc.setEnabled(false);
		}
	}

	/**
	 * @param toExportList
	 * @param xlsFile
	 */
	private void writeToFile(List<EmployeeDTO> toExportList, File xlsFile) {
		try {

			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("en", "EN"));
			WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, ws);

			WritableSheet sheet = workbook.createSheet("Employees", 0);

			WritableFont font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD);
			WritableCellFormat format = new WritableCellFormat(font);

			sheet.setColumnView(1, 5);
			sheet.setColumnView(2, 20);
			sheet.setColumnView(3, 15);
			sheet.setColumnView(4, 30);
			sheet.setColumnView(5, 30);
			sheet.setColumnView(6, 20);

			format.setBackground(Colour.ICE_BLUE);
			format.setAlignment(Alignment.CENTRE);
			format.setBorder(Border.ALL, BorderLineStyle.THIN);

			sheet.addCell(new Label(1, 3, "", format));
			sheet.addCell(new Label(2, 3, "Name", format));
			sheet.addCell(new Label(3, 3, "Birthday", format));
			sheet.addCell(new Label(4, 3, "Address", format));
			sheet.addCell(new Label(5, 3, "Email", format));
			sheet.addCell(new Label(6, 3, "Phone", format));

			font = new WritableFont(WritableFont.ARIAL, 24, WritableFont.NO_BOLD);
			font.setColour(Colour.DARK_PURPLE);
			format = new WritableCellFormat(font);
			format.setAlignment(Alignment.CENTRE);

			sheet.addCell(new Label(4, 1, "Search Result", format));

			font = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD);

			for (int i = 0; i < toExportList.size(); i++) {

				format = new WritableCellFormat(font);
				format.setAlignment(Alignment.LEFT);
				format.setBorder(Border.ALL, BorderLineStyle.THIN);
				format.setOrientation(Orientation.HORIZONTAL);
				if (i % 2 == 1) {
					format.setBackground(Colour.IVORY);
				}
				else {
					format.setBackground(Colour.WHITE);
				}

				sheet.addCell(new Label(1, i + 4, String.valueOf(i + 1), format));
				sheet.addCell(new Label(2, i + 4, toExportList.get(i).getName(), format));
				sheet.addCell(new Label(3, i + 4, toExportList.get(i).getBirthday(), format));
				sheet.addCell(new Label(4, i + 4, toExportList.get(i).getAddress(), format));
				sheet.addCell(new Label(5, i + 4, toExportList.get(i).getEmail(), format));
				sheet.addCell(new Label(6, i + 4, toExportList.get(i).getPhone(), format));
			}

			workbook.write();
			workbook.close();
		}
		catch (Exception fileEx) {
			log.info("ExportEmpInfoDAO.writeToFile: " + fileEx.toString());
		}
	}

	
}


class ExcelFileFilter extends FileFilter {

	@Override
	// Accept all directories and all xls files.
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}

		String extension = getExtension(f);
		if (extension != null) {
			if (extension.equals("xls")) {
				return true;
			}
			else {
				return false;
			}
		}

		return false;
	}

	/*
	 * Get the extension of a file.
	 */
	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 && i < s.length() - 1) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}

	@Override
	public String getDescription() {
		return "Excel 97-2003 Workbook";
	}
}
